package com.ltx.controller;

import com.ltx.pojo.Member;
import com.ltx.service.MemberService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    //调用Service层
    private final MemberService memberService;

    public MemberController(@Qualifier("memberServiceImpl") MemberService memberService) {
        this.memberService = memberService;
    }

    //查询所有用户并返回到一个界面
    @RequestMapping("/allMember")
    public String list(Model model){
        List<Member> members=memberService.queryMember();
        int man = memberService.manCount();
        int woman = memberService.womanCount();
        model.addAttribute("man",man);
        model.addAttribute("woman",woman);
        model.addAttribute("list",members);
        return "allMember";
    }
    //跳转到新增会员页面
    @RequestMapping("/toAddMember")
    public String toAddMember(){
        return "addMember";
    }
    @RequestMapping("addMember")
    public String AddMember(int mId,String mSex,String mBrithday,String mName) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(mBrithday);
        Member member = new Member();
        member.setmBrithday(parse);
        member.setmId(mId);
        member.setmName(mName);
        member.setmSex(mSex);
        memberService.addMember(member);
        return "redirect:/member/allMember";
    }
    //跳转到修改会员界面
    @RequestMapping("toUpdateMember")
    public  String toUpdateMember(String mName,Model model){
        Member member = memberService.queryByName(mName);
        model.addAttribute("members",member);
        return "UpdateMember";
    }
    //修改会员界面
    @RequestMapping("UpdateMember")
    public  String UpdateMember(int mId,String mSex,String mBrithday,String mName) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(mBrithday);
        Member member = new Member();
        member.setmBrithday(parse);
        member.setmId(mId);
        member.setmName(mName);
        member.setmSex(mSex);
        memberService.updateMember(member);
        return "redirect:/member/allMember";
    }
    //删除会员
    @RequestMapping("deleteMember")
    public String deleteMember(int mId)
    {
        memberService.deleteMember(mId);
        return "redirect:/member/allMember";
    }
    //根据名字查询会员
    @RequestMapping("queryMember")
    public  String queryMember(String mName,Model model){
        Member member = memberService.queryByName(mName);
        List<Member> members=new ArrayList<Member>();
        members.add(member);
        if(member==null){
            members=memberService.queryMember();
            model.addAttribute("error","未查询到");
        }
        model.addAttribute("list",members);
        return "allMember";
    }

}
